package com.firefly.service.repository.ums;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.firefly.dao.assembler.ums.DepartmentAssembler;
import com.firefly.dao.dao.ums.po.DepartmentAdminPo;
import com.firefly.dao.dao.ums.po.DepartmentPo;
import com.firefly.dao.dao.ums.service.DepartmentAdminService;
import com.firefly.dao.dao.ums.service.DepartmentService;
import com.firefly.util.utils.TreeNodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/11/30
 **/
@Repository
public class DepartmentRepository {
    private final DepartmentService departmentService;
    private final DepartmentAdminService departmentAdminService;
    private final DepartmentAssembler departmentAssembler;

    @Autowired
    public DepartmentRepository(DepartmentService departmentService, DepartmentAdminService departmentAdminService, DepartmentAssembler departmentAssembler) {
        this.departmentService = departmentService;
        this.departmentAdminService = departmentAdminService;
        this.departmentAssembler = departmentAssembler;
    }

    public boolean createEntity(DepartmentPo entity) {
        return departmentService.save(entity);
    }

    public boolean updateById(DepartmentPo entity) {
        return departmentService.updateById(entity);
    }

    public boolean changeStatusById(DepartmentPo entity) {
        UpdateWrapper<DepartmentPo> update = new UpdateWrapper<>();
        update.eq("id",entity.getId());
        update.set("status",entity.getStatus());
        return departmentService.update(update);
    }
    public List<Integer> getIds(Integer id){
        // 查所有菜单出来
		List<DepartmentPo> list = departmentService.list(null);
		// 置空所有ids的存放空间
		List<Integer> ids = TreeNodeUtils.getIds(id,list.stream().collect(Collectors.toList()));
        ids.add(id);
        return ids;
    }
    //	删除单条
    public boolean removeId(List<Integer> ids) {
        return departmentService.removeByIds(ids);
    }

    // 移出用户
    public boolean removeAdmin(int departmentId, int adminId) {
        QueryWrapper<DepartmentAdminPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", departmentId);
        queryWrapper.eq("admin_id", adminId);
        Boolean result = departmentAdminService.remove(queryWrapper);
        return result;
    }
}
